package com.lbssoft.gpsbd.exception;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ValidationException;

import com.lbssoft.gpsbd.common.ErrorEnum;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.validation.FieldError;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.lbssoft.gpsbd.model.ArgumentInvalidResult;
import com.lbssoft.gpsbd.model.Result;

@ControllerAdvice  
//如果返回的为json数据或其它对象，添加该注解  
@ResponseBody
@Component
public class GlobalExceptionHandler {  
	

	  @Bean
	  public MethodValidationPostProcessor methodValidationPostProcessor() {
	      return new MethodValidationPostProcessor();
	  }

	  @ExceptionHandler
	  @ResponseBody
	  @ResponseStatus(HttpStatus.BAD_REQUEST)
	  public Result handle(ValidationException exception) {
		  Result resultMsg = new Result(1, "参数错误", exception.getMessage());
	      return resultMsg;
	  }

	
	
  //添加全局异常处理流程，根据需要设置需要处理的异常，本文以MethodArgumentNotValidException为例  
  @ExceptionHandler(value=MethodArgumentNotValidException.class)  
  public Object MethodArgumentNotValidHandler(HttpServletRequest request,  
          MethodArgumentNotValidException exception) throws Exception  
  {  
      //按需重新封装需要返回的错误信息  
      List<ArgumentInvalidResult> invalidArguments = new ArrayList<>();  
      //解析原错误信息，封装后返回，此处返回非法的字段名称，原始值，错误信息  
      for (FieldError error : exception.getBindingResult().getFieldErrors()) {  
          ArgumentInvalidResult invalidArgument = new ArgumentInvalidResult();  
          invalidArgument.setDefaultMessage(error.getDefaultMessage());  
          invalidArgument.setField(error.getField());  
          invalidArgument.setRejectedValue(error.getRejectedValue());  
          invalidArguments.add(invalidArgument);  
      }  
        
      Result resultMsg = new Result(ErrorEnum.E_002.getErrorCode(), ErrorEnum.E_002.getErrorMsg(), invalidArguments);
      return resultMsg;  
  }

    @ExceptionHandler(value=UnauthorizedException.class)
    public Object UnauthorizedExceptionHandler(HttpServletRequest request,  Exception exception) throws Exception
    {

        Result resultMsg = new Result(ErrorEnum.E_003.getErrorCode(), ErrorEnum.E_003.getErrorMsg(), exception.getMessage());
        return resultMsg;
    }
  
  
  @ExceptionHandler(value=Exception.class)  
  public Object Handler(HttpServletRequest request,  Exception exception) throws Exception  
  {
      Result resultMsg = new Result(ErrorEnum.E_004.getErrorCode(), ErrorEnum.E_004.getErrorMsg(), exception.getMessage());
      return resultMsg;  
  }


}  
